package com.guoning.office_system.controller;

import com.guoning.office_system.model.SignInWithBLOBs;
import com.guoning.office_system.model.User;
import com.guoning.office_system.service.SignInService;
import com.guoning.office_system.utils.Constants;
import com.guoning.office_system.utils.DateFormatUtils;
import com.guoning.office_system.utils.GetIpAddrUtil;
import com.guoning.office_system.utils.JsonResult;
import com.guoning.office_system.vo.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/signIn")
public class SignInController extends BaseController {
    @Autowired
    SignInService signInService;

    @RequestMapping(value = "/selectRecord")
    public JsonResult selectRecordByUser(HttpServletRequest request,
                                         @RequestParam(value = "date", defaultValue = "") String date,
                                         @RequestParam(value = "day", defaultValue = "") String day,
                                         @RequestParam(value = "signStatus", defaultValue = "") Integer signStatus){
        JsonResult jsonResult;
        try {
            User user = getUserDate(request);
            Criteria criteria = new Criteria();
            criteria.addExtField("userId", user.getId());
            criteria.addExtField("signStatus", signStatus);
            if (day.equals("") || day == null){
                if(date.equals("") || date == null){
                    date = DateFormatUtils.format(new Date(), DateFormatUtils.DATE_MODEL_6);
                }
                criteria.addExtField("date", date);
            }else{
                criteria.addExtField("day", date + "-" + day);
            }
            List<SignInWithBLOBs> list = signInService.selectRecordByUser(criteria);
            if(list.size()>0){
                jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "查询成功", list);
            }else{
                jsonResult = new JsonResult(Constants.STATUS_FAIL, "暂无考勤记录");
            }
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "查询异常");
        }
        return jsonResult;
    }

    /**
     * 打卡/补签
     * @param request
     * @param sign
     * @return
     */
    @RequestMapping(value = "/insert")
    public JsonResult insert(HttpServletRequest request, SignInWithBLOBs sign){
        JsonResult jsonResult;
        try {
            User user = getUserDate(request);
            sign.setUserId(user.getId());
            sign.setSignIp(GetIpAddrUtil.getIpAddr(request));
            sign.setCreator(user.getUserName());
            Criteria criteria = new Criteria();
            criteria.addExtField("userId", user.getId());
            if(sign.getTs() == null){
                sign.setTs(new Date());
            }
            criteria.addExtField("day", DateFormatUtils.format(sign.getTs(), DateFormatUtils.DATE_MODEL_5));
            List<SignInWithBLOBs> list = signInService.selectRecordByUser(criteria);
            if(list.size() != 0)
                return new JsonResult(Constants.STATUS_FAIL, "已打卡，请勿重复操作");
            int i = signInService.insert(sign);
            if(i == 0){
                jsonResult = new JsonResult(Constants.STATUS_FAIL, "打卡失败");
                return jsonResult;
            }
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "打卡成功");
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_FAIL, "操作异常");
        }
        return jsonResult;
    }
}
